function openAjax() {
    var ajax;

    try{
        ajax = new XMLHttpRequest(); // XMLHttpRequest para Firefox, Safari, dentre outros.
    } catch(ee){
        try{
            ajax = new ActiveXObject("Msxml2.XMLHTTP"); // Para o Internet Explorer
        } catch(e){
            try{
                ajax = new ActiveXObject("Microsoft.XMLHTTP"); // Para o Internet Explorer
            } catch(E){
                ajax = false;
            }
        }
    }
    return ajax;
}

function login()
{
    var ajax = openAjax();

    var inputLogin = document.getElementById("userLogin");
    var inputPassword = document.getElementById("passwordLogin");
    var btnSubmit = document.getElementById("btnSubmit");

    var divInner = document.getElementById("inner");
    ajax.open("GET", "login.jsp?login="+inputLogin.value+"&password="+inputPassword.value, true);
    
    ajax.onreadystatechange = function()
    {
        if(ajax.readyState == 1) //carregando...
        {
            inputLogin.disabled = true;
            inputPassword.disabled = true;
            btnSubmit.disabled = true;
        }
        if(ajax.readyState == 4) //ok
        {
            var result = ajax.responseText;
            divInner.innerHTML = result;

            inputLogin.value = "";
            inputPassword.value = "";

            inputLogin.disabled = false;
            inputPassword.disabled = false;
            btnSubmit.disabled = false;

            $(document).ready(function() {
                $("#panel").hide();
            });
        }
    }
    
    ajax.send(null); // submete
}

function loadLog()
{
    var ajax = openAjax();

    var user = document.getElementById("user").value;

    if (user != "") {
        var divLog = document.getElementById("log");
        ajax.open("GET", "log/"+user+"/log.html", true);

        ajax.onreadystatechange = function()
        {
            if(ajax.readyState == 1) //carregando...
            {

            }
            if(ajax.readyState == 4) //ok
            {
                var result = ajax.responseText;
                divLog.innerHTML = result;
            }
        }

        ajax.send(null); // submete
    }
}

function onLoadLogin()
{
    setInterval("loadLog()",2000);
}

function openPage(page)
{
    var ajax = openAjax();
    var inner = document.getElementById('inner');
    var url = page + ".jsp";

    ajax.open("GET", url, true);
    ajax.onreadystatechange = function()
    {
        if(ajax.readyState == 1) //carregando...
        {
        }
        if(ajax.readyState == 4) //ok
        {
            var result = ajax.responseText;
            inner.innerHTML = result;
        }
    }

    ajax.send(null); // submete
}

